package com.jyxd.web.service.assessment.basic;

import com.jyxd.web.dao.basic.BreatheSystemFormDao;
import com.jyxd.web.data.basic.BreatheSystemForm;
import com.jyxd.web.data.dto.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.jyxd.web.util.DateUtil.*;
import static com.jyxd.web.util.ExecuteUtil.errorIfEmpty;
import static com.jyxd.web.util.UUIDUtil.getUUID;

@Service
public class BreatheSystemFormService {

    @Autowired
    private BreatheSystemFormDao breatheSystemFormDao;

    public HttpResult saveOrUpdate(BreatheSystemForm breatheSystemForm) {
        errorIfEmpty(breatheSystemForm, "数据不存在");
        if (Objects.isNull(breatheSystemForm.getId())) {
            breatheSystemForm.setId(getUUID());
            breatheSystemForm.setStatus("1");
            breatheSystemFormDao.insert(breatheSystemForm);
        } else {
            breatheSystemFormDao.updateById(breatheSystemForm);
        }
        return new HttpResult(200, "数据新增修改成功");
    }

    public HttpResult deleteById(String id) {
        errorIfEmpty(id, "数据id不能为空");
        BreatheSystemForm breatheSystemForm = breatheSystemFormDao.selectById(id);
        errorIfEmpty(breatheSystemForm, "数据不存在");
        breatheSystemForm.setStatus("-1");
        breatheSystemFormDao.updateById(breatheSystemForm);
        return new HttpResult(200, "数据删除成功");
    }

    public HttpResult getListPage(Map<String, Object> map) throws Exception {
        errorIfEmpty(map, "参数不存在");
        errorIfEmpty(map.get("patientId"), "患者id不存在");
        if (map.containsKey("start") && map.containsKey("size")) {
            Long start = (Long) map.get("start");
            Long size = (Long) map.get("size");
            start = (start - 1) * size;
            map.put("start", start);
        }
        if (map.containsKey("hour")) {
            Date endTime = getLaterHoursDate(yyyyMMddHHmmssSdfToDate(String.valueOf(map.get("startTime"))), Long.valueOf(String.valueOf(map.get("hour"))));
            map.put("endTime", yyyyMMddHHmmssSdfToString(endTime));
        }
        List<Map<String, Object>> list = breatheSystemFormDao.selectByDataTime(map);
        return new HttpResult(200, list, breatheSystemFormDao.selectByDataTimeCount(map), "分页数据查询成功");
    }
}
